package org.wtframework.platform.server.model.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Proxy;
import org.openredhead.springx.core.server.model.IUser;

@Entity
@Proxy(lazy = true)
@DynamicInsert(value = true)
@DynamicUpdate(value = true)
@Table(name = "orh_sx_user", uniqueConstraints = { @UniqueConstraint(columnNames = { "username", "email" }) })
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User implements IUser {

	private static final long serialVersionUID = -6700287574180218179L;

	private Long id;

	// 用户名
	private String username;

	// 用户呢称
	private String nickname;

	// 密码
	private String password;

	// 邮件地址
	private String email;

	// 手机号码
	private String phoneNumber;

	// 角色ids
	private List<Integer> rolesId;

	// 用户组id
	private Integer parentId;

	// 身份
	private Integer identityId;

	// 头像
	private String imageUrl;

	// 可用积分
	private Double unableScore = 0d;

	// 创建时间
	private Date establishDate = new Date();

	// 排序id
	private Long orderId;

	// 是否可用
	private Boolean locked = false;

	// 是否设置过期
	private Boolean isSetExpiredTime = false;

	// 过期时间
	private Date expiredDate = new Date();

	// 设置审批
	private Integer approval = 0; // 0:未审批 ，1，审批通过

	// 最近登录时间
	private Date loginTime;
	

	public void setId(Long id) {
		this.id = id;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setLocked(Boolean locked) {
		this.locked = locked;
	}

	public Boolean getLocked() {
		return locked;
	}

	public void setIsSetExpiredTime(Boolean isSetExpiredTime) {
		this.isSetExpiredTime = isSetExpiredTime;
	}

	@Column(name = "is_set_expired_time")
	public Boolean getIsSetExpiredTime() {
		return isSetExpiredTime;
	}

	public void setExpiredDate(Date expiredDate) {
		this.expiredDate = expiredDate;
	}

	@Column(name = "expired_date")
	public Date getExpiredDate() {
		return expiredDate;
	}

	public void setRolesId(List<Integer> rolesId) {
		this.rolesId = rolesId;
	}

	@ElementCollection(fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	@JoinTable(name = "orh_sx_link_user_role", joinColumns = @JoinColumn(name = "user_id"))
	@Column(name = "role_id")
	public List<Integer> getRolesId() {
		return rolesId;
	}

	public void setOrderId(Long orderId) {
		this.orderId = orderId;
	}

	@Column(name = "order_id")
	public Long getOrderId() {
		return orderId;
	}

	@Column(name = "parent_id")
	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	public void setEstablishDate(Date establishDate) {
		this.establishDate = establishDate;
	}

	@Column(name = "establish_date")
	public Date getEstablishDate() {
		return establishDate;
	}

	@Column(name = "identity_id")
	public Integer getIdentityId() {
		return identityId;
	}

	public void setIdentityId(Integer identityId) {
		this.identityId = identityId;
	}

	@Column(name = "image_url")
	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "phone_number")
	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	@Column(name = "unable_score")
	public Double getUnableScore() {
		return unableScore;
	}

	public void setUnableScore(Double unableScore) {
		this.unableScore = unableScore;
	}

	@Column(name = "login_time")
	public Date getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}

	public Integer getApproval() {
		return approval;
	}

	public void setApproval(Integer approval) {
		this.approval = approval;
	}

	@Transient
	public boolean isAccountNonExpired() {

		if (getIsSetExpiredTime() == null) return false;

		if (!getIsSetExpiredTime()) return true;

		if (getIsSetExpiredTime()) return new Date().before(getExpiredDate());

		return false;

	}

	@Transient
	public boolean isAccountNonLocked() {

		return !getLocked();

	}

	@Transient
	public boolean isCredentialsNonExpired() {

		return true;

	}

	@Transient
	public boolean isEnabled() {

		return (isAccountNonLocked() && isAccountNonExpired() && isCredentialsNonExpired() && (getApproval() == 1));

	}

	@Override
	public boolean equals(Object o) {

		if (o == null) return false;

		if (!(o instanceof User)) return false;

		final User instance = (User) o;

		if (getId() != null ? !getId().equals(instance.getId()) : instance.getId() != null) return false;

		return true;

	}

	@Override
	public int hashCode() {

		return (getId() != null ? getId().hashCode() : 0);

	}

}
